
$(function() {
	
	$("input[id^='name_s']:visible").autocomplete(Test.supplier_name);
/*
	$("select[id^='category_']:visible").selectChain({
		target: $("select[id^='type_']:visible"),
		key: "code",
		value: "name",
		url: 'contact.php?do=Supplier.ajax_category',
		data: { ajax: true }
	}).trigger('change');*/
	
	Tip.emp('负责人');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("负责人：");
		} else if (a == 'oper') {
			label.html("录入员：");
		}
	});
});


var Supplier = {
	dialog_add_22: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
	
				if (!Contact.validate_add()) {
					return false;
				}
	
				var linkman = $("input[name='linkman']");
				var job = $("input[name='job']");
				var tel = $("input[name='tel']");
				var fax = $("input[name='fax']");
				var mobile = $("input[name='mobile']");
				var dh = $("input[name='dh']");
				
				var email = $("input[name='email']");
				var address = $("input[name='address']");
				
				var qq = $("input[name='qq']");
				var skype = $("input[name='skype']");
				var content = $("input[name='content']");
	
				var aa = new Array();
				for (var i = 0; i < linkman.length; i++) {
					var contact = {};
					contact.linkman = $(linkman[i]).val();
					contact.job = $(job[i]).val();
					contact.tel = $(tel[i]).val();
					contact.fax = $(fax[i]).val();
					
					contact.mobile = $(mobile[i]).val();
					contact.dh = $(dh[i]).val();
					
					contact.email = $(email[i]).val();
					contact.address = $(address[i]).val();
					
					contact.qq = $(qq[i]).val();
					contact.skype = $(skype[i]).val();
					contact.content = $(content[i]).val();
					
					aa.push(contact);
				}
	
				var form = $("#form_add")[0];
	
				var body = { };
				body.name = form.name.value;			
				body.fullname = form.fullname.value;
				
				body.category = form.category.value;
				body.type = form.type.value;
		
				body.address = form.address.value;
				
				body.pay = form.pay.value;
				body.currency = form.currency.value;
				body.tax = form.tax.value;
				body.url = form.url.value;
				body.share = form.share.value;
				
				body.jj = form.jj.value;
				body.content = form.content.value;
	
				body.contact = $.toJSON(aa);
				body.emp = form.emp.value;
				
				$(this).dialog("close");
	
				$.post('contact.php?do=Supplier.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
	
				var form = $("#form_add")[0];
	
				var body = { };
				body.name = form.name.value;			
				body.fullname = form.fullname.value;
				body.cert_type = form.cert_type.value;
				body.cert_no = form.cert_no.value;
		
				body.address = form.address.value;
				body.url = form.url.value;
				body.jj = form.jj.value;
	
				body.emp = form.emp.value;
				
				$(this).dialog("close");
	
				$.post('contact.php?do=Supplier.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);
	
		p2 = query(p2);
		$.cookie('p2', p2);
	
		var c1 = 'contact.php?do=Supplier.edit&id=' + id;
		var c2 = 'contact.php?do=Supplier.view&id=' + id;
		var c3 = 'contact.php?do=Supplier.copy&id=' + id;
	
		var c4 = 'info.php?do=Contact.add&info=3&id=' + id;
		var c5 = 'contact.php?do=Supplier.bank&id=' + id;
		var c6 = 'contact.php?do=Supplier.upload&id=' + id;
		
		var c8 = 'contact.php?do=Supplier.merge&id=' + id;
		
		var a1 = ['编辑信息', 'url', 'Supplier.dialog_edit_', '650x450'];	
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x420'];
		var a3 = ['复制信息', 'url', 'Supplier.dialog_copy_', '650x420'];
		
		var a4 = ['联系信息', 'url', 'Supplier.dialog_add_contact', '500x300'];
		var a5 = ['银行帐号', 'url', 'Supplier.dialog_bank', '650x450'];
		
		var a6 = ['上传附件', 'url', 'Common.dialog_upload', '650x450'];
		
		var a8 = ['合并信息', 'url', 'Supplier.dialog_merge', '650x450'];
	
		var s = c1 + ", " + c2 + ", " + c3 + ", " + c5 + ", " + c6 + ", " + c8;
	
		return this.popup_(e, [a1, a2, a3, a5, a6, a8], s);
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'contact.php?do=Supplier.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Supplier.dialog_ss', '400x200'];
		
		s += ", " + ss;
		a.push(bb);
		
		return popup(e, a, s);
	},
	
	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('contact.php?do=Supplier.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},
	
					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
	
						var form = $("#form_edit")[0];
	
						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;
	
						$(this).dialog("close");
	
						var url = 'contact.php?do=Supplier.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
	
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
	
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('contact.php?do=Supplier.audit', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {			
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
									
						var id = $("input[name='id']");
						var linkman = $("input[name='linkman']");
						var job = $("input[name='job']");
						var dept = $("input[name='dept']");
						
						var tel = $("input[name='tel']");
						var fax = $("input[name='fax']");
						
						var mobile = $("input[name='mobile']");
						var dh = $("input[name='dh']");
						
						var email = $("input[name='email']");
						var address = $("input[name='address']");
						var geo = $("input[name='geo']");
						
						var qq = $("input[name='qq']");
						var skype = $("input[name='skype']");
						var content = $("input[name='content']");
			
						var aa = new Array();
						for (var i = 0; i < z.length; i++) {
							var contact = {};
							var b = $("#form" + z[i] + " :input[name='id']");
							if (b != null) {
								contact.id = $("#form" + z[i] + " :input[name='id']").val();
							}			
			
							contact.linkman = $(linkman[i]).val();
							contact.job = $(job[i]).val();
							contact.dept = $(dept[i]).val();
							
							contact.tel = $(tel[i]).val();
							contact.fax = $(fax[i]).val();
							contact.mobile = $(mobile[i]).val();
							contact.dh = $(dh[i]).val();
							
							contact.email = $(email[i]).val();
							contact.address = $(address[i]).val();
							contact.geo = $(geo[i]).val();
						
							contact.qq = $(qq[i]).val();
							contact.skype = $(skype[i]).val();
							contact.content = $(content[i]).val();
							
							aa.push(contact);
						}
			
						var form = $("#form_edit")[0];
			
						var body = { };
						body.id = form.info_id.value;
						body.name = form.name.value;
						body.fullname = form.fullname.value;
						body.star = form.star.value;
						
						body.cert_type = form.cert_type.value;
						body.cert_no = form.cert_no.value;
			
						body.category = form.category.value;
						body.type = form.type.value;
						
						body.address = form.address.value;
						body.geo = form.main_geo.value;
						body.url = form.url.value;
						
						body.pay = form.pay.value;
						body.currency = form.currency.value;
						body.tax = form.tax.value;
						
						body.share = form.share.value;
						
						body.jj = form.jj.value;
						body.content = form.content.value;
						body.emp = form.emp.value;
						body.status = form.status.value;
			
						body.contact = $.toJSON(aa);
			
						body.z = $.cookie('contact');
			
						$.cookie('contact', null);
						
						$(this).dialog("close");
			
						var url = 'contact.php?do=Supplier.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
			
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
			
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {			
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
							
				if (!ContactEdit.validate_edit()) {
					return false;
				}
							
				var id = $("input[name='id']");
				var linkman = $("input[name='linkman']");
				var job = $("input[name='job']");
				var tel = $("input[name='tel']");
				var fax = $("input[name='fax']");
				
				var mobile = $("input[name='mobile']");
				var dh = $("input[name='dh']");
				
				var email = $("input[name='email']");
				var address = $("input[name='address']");
				
				var qq = $("input[name='qq']");
				var skype = $("input[name='skype']");
				var content = $("input[name='content']");
	
				var aa = new Array();
				for (var i = 0; i < z.length; i++) {
					var contact = {};
					var b = $("#form" + z[i] + " :input[name='id']");
					if (b != null) {
						contact.id = $("#form" + z[i] + " :input[name='id']").val();
					}			
	
					contact.linkman = $(linkman[i]).val();
					contact.job = $(job[i]).val();
					contact.tel = $(tel[i]).val();
					contact.fax = $(fax[i]).val();
					contact.mobile = $(mobile[i]).val();
					contact.dh = $(dh[i]).val();
					
					contact.email = $(email[i]).val();
					contact.address = $(address[i]).val();
					
					contact.qq = $(qq[i]).val();
					contact.skype = $(skype[i]).val();
					contact.content = $(content[i]).val();
					
					aa.push(contact);
				}
	
				var form = $("#form_edit")[0];
	
				var star = $("input[name='star']:checked");
	
				var body = { };
				body.id = form.info_id.value;
				body.name = form.name.value;
				body.fullname = form.fullname.value;
				
				body.cert_type = form.cert_type.value;
				body.cert_no = form.cert_no.value;
				
				body.star = star.val();
	
				body.category = form.category.value;
				body.type = form.type.value;
				
				body.address = form.address.value;
				
				body.pay = form.pay.value;
				body.currency = form.currency.value;
				body.tax = form.tax.value;
				body.url = form.url.value;
				body.share = form.share.value;
				
				body.jj = form.jj.value;
				body.content = form.content.value;
				body.emp = form.emp.value;
				body.status = form.status.value;
	
				body.contact = $.toJSON(aa);
	
				body.z = $.cookie('contact');
	
				$.cookie('contact', null);
				
				$(this).dialog("close");
	
				var url = 'contact.php?do=Supplier.addPost';
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},
	
	dialog_add_contact: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
				
				var form = $("#form_add")[0];
	
				var body = { };
				body.info = form.info.value;
				body.info_id = form.info_id.value;
				body.name = form.name.value;
	
	
				body.tel = form.tel.value;
				body.fax = form.fax.value;
				body.mobile = form.mobile.value;
				body.email = form.email.value;
				body.im = form.im.value;
	
				$(this).dialog("close");
	
				$.post('info.php?do=Contact.addPost', body, function(data) {
					alert("添加成功！");
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	dialog_bank: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {			
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
							
				var form = $("#form_add")[0];
	
				var body = { };
				body.id = form.id.value;
				
				body.category = form.category.value;
				body.bank = form.bank.value;
				body.branch = form.branch.value;
				body.bankcode = form.bankcode.value;
	
				body.province = form.province.value;
				body.city = form.city.value;
				
				body.zone = form.zone.value;
				body.type = form.type.value;
				
				body.swift = form.swift.value;
				body.name = form.name.value;
				body.code = form.code.value;
				
				body.content = form.content.value;
				
				var url = 'contact.php?do=Supplier.ajax_bank';
				$.getJSON(url, body, function(json) {
					if (json.status == '1') {
						$.post('contact.php?do=Supplier.bankPost', body, function(data) {
							$('#dialog_add').html(data);
						});
					} else {
						// alert($.toJSON(json));
						alert("帐号信息已存在，请联系管理员！");
					}
					
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	dialog_merge: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('contact.php?do=Supplier.audit', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {			
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
			
						var form = $("#form_edit")[0];
			
						var body = { };
						body.id = form.info_id.value;
						body.to = form.to.value;
						
						$(this).dialog("close");
			
						var url = 'contact.php?do=Supplier.mergePost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
			
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
			
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	download: function (key) {
		
		if (!confirm("确认是否下载？若下载，建议先另存，再打开。")) {
			return false;
		}
		
		var url = 'car.php?do=Car.download&key=' + key;
		jswin(url, 'save', '800x600');
		
		return true;
	},
	
	file: function (id, key) {
		if (!confirm("确认是否删除？")) {
			return false;
		}
		
		var c6 = 'contact.php?do=Supplier.upload&id=' + id;
		
		var url = 'contact.php?do=Supplier.file';
		var body = { };
		body.key = key;
		
		$.post(url, body, function(data) {
			// alert(data);
			$.get(c6, function(data) {
				$('#dialog_edit').html(data);
			});
		});
		
		return true;
	},
	
	expunge_: function (form) {
		$.getJSON('contact.php?do=Supplier.audit&item=11', function(json) {
			if (json.status == 1) {
				expunge(form);
			} else {
				_error();
			}
		});
	},
	
	supplier_x: function(form) {
		
		var supplier_x = {};				
		supplier_x.name = form.c_name.value;
		supplier_x.fullname = form.c_fullname.value;
		supplier_x.address = form.c_address.value;				
		supplier_x.web = form.c_web.value;
		
		supplier_x.contact = form.c_contact.value;
		supplier_x.mobile = form.c_mobile.value;
		supplier_x.tel = form.c_tel.value;
		supplier_x.fax = form.c_fax.value;
		supplier_x.email = form.c_email.value;
		supplier_x.content = form.c_content.value;		
		
		return $.toJSON(supplier_x);
	},
	
	linkman: function (a) {		
		$select = $("#supplier_linkman");		
		$select.empty();
		count = 0;
		$.each(a, function(k, v) {
	    	var item = new Option(v.name, v.id);
	    	$select[0].options.add(item);
	    	
	    	if (count == 1) {
	    		Supplier._link_man(v.id, false);
	    	}
	    	
	    	count++;
	    });
		
		$select.change(function() {
			var id = $(this).val();
			Supplier._link_man(id, true);
		}); 
	},

	_link_man: function (id, select) {
		var url = 'info.php?do=Contact.ajax_id&q=' + id;
		$.getJSON(url, function(json) {
			var aaa = Supplier.aaa();
			if (select || aaa) {
				$("#c_contact").val(json.name);
				$("#c_mobile").val(json.mobile);
				$("#c_tel").val(json.tel);
				$("#c_fax").val(json.fax);
				$("#c_email").val(json.email);
			}
		});
	},
	
	aaa: function() {
		if ($("#c_contact").val().trim() != "") {
			return false;
		}
		 
		if ($("#c_mobile").val().trim() != "") {
			return false;
		} 
		
		if ($("#c_tel").val().trim() != "") {
			return false;
		}
		
		if ($("#c_fax").val().trim() != "") {
			return false;
		}
		
		if ($("#c_email").val().trim() != "") {
			return false;
		}
		
		return true;
	}
};
